//
//  HtmlHelper.swift
//  HuaDaoPeifu
//
//  Created by 刘杰 on 2018/7/30.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

import UIKit

class HDHtmlHelper: NSObject {
    static func policyBillTableHtml(_ payModels:[HDPolicyBillModel],_ nopayModels:[HDPolicyBillModel]) -> (payHtml:String,nopayHtml:String) {
        
        guard let payFilePath = Bundle.main.path(forResource: "case_detail_bill_pay", ofType: "html"),
        let nopayFilePath = Bundle.main.path(forResource: "case_detail_bill_nopay", ofType: "html")
        else {
            return ("","")
        }
        
        var payHtml = try? String(contentsOfFile: payFilePath)
        var nopayHtml = try? String(contentsOfFile: nopayFilePath)
        
        var payTrStr_part1 = ""
        var payTrStr_part2 = ""
        for payModel in payModels {
            payTrStr_part1.append("<tr>")
            payTrStr_part1.append("<td>\(payModel.DUTYTYPE)</td>")
            payTrStr_part1.append("<td>\(String(format: "%.2f",Double(payModel.TOTALAMNT) ?? 0.00))</td>")
            payTrStr_part1.append("<td>\(String(format: "%.2f",Double(payModel.SOCIALAMNT) ?? 0.00))</td>")
            payTrStr_part1.append("<td>\(String(format: "%.2f",Double(payModel.SELFPAYAMNT) ?? 0.00))</td>")
            payTrStr_part1.append("<td>\(String(format: "%.2f",Double(payModel.NONPAYAMNT) ?? 0.00))</td>")
            payTrStr_part1.append("<td>\(String(format: "%.2f",Double(payModel.OWNAMNT) ?? 0.00))</td></tr>")
            
            payTrStr_part2.append("<tr><td>\(payModel.DUTYTYPE)</td>")
            payTrStr_part2.append("<td>\(String(format: "%.2f",Double(payModel.CANPAYAMNT) ?? 0.00))</td>")
            payTrStr_part2.append("<td>\(String(format: "%.2f",Double(payModel.Deductibleamnt) ?? 0.00))</td>")
            payTrStr_part2.append("<td>\(payModel.PAYRATIO)</td>")
            payTrStr_part2.append("<td>\(String(format: "%.2f",Double(payModel.PAYAMNT) ?? 0.00))</td>")
            payTrStr_part2.append("<td></td>")
            payTrStr_part2.append("</tr>")
        }

        payHtml = payHtml?.replacingOccurrencesOfString("#part1#", withString: payTrStr_part1)
        payHtml = payHtml?.replacingOccurrencesOfString("#part2#", withString: payTrStr_part2)
        
        var nopayTrStr_part1 = ""
        var nopayTrStr_part2 = ""
        for nopayModel in nopayModels {
            nopayTrStr_part1.append("<tr>")
            nopayTrStr_part1.append("<td>\(nopayModel.BILLTYPE)</td>")
            nopayTrStr_part1.append("<td>\(nopayModel.BILLReceiptNo)</td>")
            nopayTrStr_part1.append("<td>\(nopayModel.hospitalname)</td>")
            nopayTrStr_part1.append("<td>\(nopayModel.billbegindate)</td>")
            nopayTrStr_part1.append("<td>\(String(format: "%.2f",Double(nopayModel.totalamnt) ?? 0.00))</td>")
            nopayTrStr_part1.append("<td>\(nopayModel.deductreason)</td>")
            nopayTrStr_part1.append("<td>\(String(format: "%.2f",Double(nopayModel.deductamnt) ?? 0.00))</td>")
            nopayTrStr_part1.append("</tr>")
            
            nopayTrStr_part2.append("<tr><td>\(nopayModel.BILLTYPE)</td>")
            nopayTrStr_part2.append("<td colspan=\"6\">\(nopayModel.deductreasonremark)</td>")
            nopayTrStr_part2.append("</tr>")
        }
        
        nopayHtml = nopayHtml?.replacingOccurrencesOfString("#part1#", withString: nopayTrStr_part1)
        nopayHtml = nopayHtml?.replacingOccurrencesOfString("#part2#", withString: nopayTrStr_part2)
        return (payHtml ?? "",nopayHtml ?? "")
    }
}
